package com.itfitness.idcardscan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    public static final int SCAN_IDCARD_REQUEST = 1;
    private TextView tvScanResult;
    private ImageView imgIdcard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AndPermission.with(this).runtime().permission(Permission.Group.CAMERA, Permission.Group.STORAGE).start();
        initView();
    }

    private void initView() {
        tvScanResult = findViewById(R.id.tv_scanResult);
        imgIdcard = (ImageView) findViewById(R.id.img_idcard);
        findViewById(R.id.bt_scan).setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, ScanIdCardActivity.class);
            startActivityForResult(intent, SCAN_IDCARD_REQUEST);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case SCAN_IDCARD_REQUEST:
                    String result = data.getStringExtra("OCRResult");
                    try {
                        JSONObject jo = new JSONObject(result);
                        StringBuffer sb = new StringBuffer();
                        sb.append(String.format("正面 = %s\n", jo.opt("type")));
                        sb.append(String.format("姓名 = %s\n", jo.opt("name")));
                        sb.append(String.format("性别 = %s\n", jo.opt("sex")));
                        sb.append(String.format("民族 = %s\n", jo.opt("folk")));
                        sb.append(String.format("日期 = %s\n", jo.opt("birt")));
                        sb.append(String.format("号码 = %s\n", jo.opt("num")));
                        sb.append(String.format("住址 = %s\n", jo.opt("addr")));
                        sb.append(String.format("签发机关 = %s\n", jo.opt("issue")));
                        sb.append(String.format("有效期限 = %s\n", jo.opt("valid")));
                        sb.append(String.format("整体照片 = %s\n", jo.opt("imgPath")));
                        sb.append(String.format("头像路径 = %s\n", jo.opt("headPath")));
                        tvScanResult.setText(sb.toString());
                        String headPath = (String) jo.opt("headPath");
                        if(!TextUtils.isEmpty(headPath)){
                            Glide.with(this).load(headPath).into(imgIdcard);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
}